
import {defineStore} from "pinia"

// defineStore(modelName,options|function)
// modelName  模块名称(唯一id)
// options/function
//        options  传入options对象 -> 使用选项式语法
//        function  传入setup函数  => 组合式语法

// 注意: 选项式语法中  this->都指向当前store实例 -> 可以直接对于属性名/方法名,获取属性/计算属性/方法
export let useMdaStore = defineStore("mda",{
    state(){
        return {
            a:1,
            b:2
        }
    },
    getters:{
        doubleA:function(state){
            return state.a*2;
        },
        doubleB:function(state){
            return state.b*2;
        },
        both:function(state){
            return state.a + state.b;
        },
        doubleBoth:function(){
            return this.doubleA + this.doubleB;
        }
    },
    actions:{
        addA(payload){ // this指向当前store实例
            console.log("payload",payload);
            this.a++;
        },
        addB(){
            this.b++;
        },
        addA_Async(){
            return new Promise((resolve)=>{
                setTimeout(()=>{
                    this.a++;
                    resolve();
                },1000)
            })
        },
        addB_Async(){
            return new Promise((resolve)=>{
                setTimeout(()=>{
                    this.b++;
                    resolve();
                },1000)
            })
        },
        async addBoth_Async(){
            await this.addA_Async();
            await this.addB_Async();
        }
    }
})